package com.ygr.anime.exception.handle;

import cn.dev33.satoken.util.SaResult;
import com.ygr.anime.exception.MyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
class GlobalExceptionHandle {

    @ExceptionHandler
    public ResponseEntity<SaResult> myExceptionHandle(MyException ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(SaResult.error()
                        .setMsg(ex.getMessage())
                        .setCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                );
    }

    @ExceptionHandler
    public ResponseEntity<SaResult> handle(Exception ex) {
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(SaResult.error()
                        .setMsg(ex.getMessage())
                        .setCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                );
    }
}
